package com.items.form.model;


import java.util.Collections;
import java.util.List;

public class Page<E> {

    public Page(){}


    public Page(long pageSize, long total, long page, long pages, boolean bool, List<E> records) {
        this.pageSize = pageSize;
        this.total = total;
        this.page = page;
        this.pages = pages;
        this.bool = bool;
        this.records = records;
    }

    private long  pageSize;

    private long total;

    private long page;

    private long pages;
    /**
     * 是否分页
     */
    private boolean bool = true;

    private List<E> records = Collections.emptyList();

    public Page(List<E> records, PageRequest pageRequest, long total){
        this.records = records;
        this.page = pageRequest.getOffset();
        this.total = total;
    }

    public static class PageRequest{

        /**
         * 当前页
         */
        private long page;

        private long  pageSize;

        /**
         * 分页偏移量
         */
        private long offset;
        public PageRequest(){}
        public PageRequest(long page, long pageSize, long offset) {
            this.page = page;
            this.pageSize = pageSize;
            this.offset = offset;
        }

        public long getPageSize() {
            return pageSize;
        }

        public PageRequest setPageSize(long pageSize) {
            this.pageSize = pageSize;
            return this;
        }

        public long getOffset() {
            long current = getPage();
            if (current <= 1L) {
                return 0L;
            }
            return (current - 1) * getPageSize();
        }

        public PageRequest setOffset(long offset) {
            this.offset = offset;
            return this;
        }

        public long getPage() {
            return page;
        }

        public PageRequest setPage(long page) {
            this.page = page;
            return this;
        }
    }

    public long getPageSize() {
        return pageSize;
    }

    public Page<E> setPageSize(long pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public long getTotal() {
        return total;
    }

    public Page<E> setTotal(long total) {
        this.total = total;
        return this;
    }

    public long getPage() {
        return page;
    }

    public Page<E> setPage(long page) {
        this.page = page;
        return this;
    }

    public Page<E> setPages(long pages) {
        this.pages = pages;
        return this;
    }

    public boolean isBool() {
        return bool;
    }

    public Page<E> setBool(boolean bool) {
        this.bool = bool;
        return this;
    }

    public List<E> getRecords() {
        return records;
    }

    public Page<E> setRecords(List<E> records) {
        this.records = records;
        return this;
    }

    /**
     * 当前分页总页数
     */
    public long getPages() {
        if (pageSize == 0) {
            return 0L;
        }
        long pages = getTotal() / pageSize;
        if (getTotal() % pageSize != 0) {
            pages++;
        }
        return pages;
    }
}
